/**
 * 载入项目引导文件
 */
require('./bootstrap');

window.Vue = require('vue');

//标签输入，官方网址：http://www.vue-tags-input.com/#/
import InputTag from 'vue-input-tag';
//时间控件，官网：https://github.com/weifeiyue/vue-datepicker-local
import VueDatepickerLocal from 'vue-datepicker-local'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

Vue.component('WebuploaderSingle', require('../components/WebuploaderSingle.vue'));
Vue.component('WebuploaderMultiple', require('../components/WebuploaderMultiple.vue'));
Vue.component('ColorSelecter', require('../components/ColorSelecter.vue'));

let box;

const app = new Vue({
    el: '#wrapper',
    data: {
        box:null,
        cardData : page_params.data_card,//卡券数据
        card_type_list:page_params.data_card_type,//卡券类型列表
        card_key_list:[],//卡券标识列表
        code_type_list:page_params.data_code_type,//码型列表
        color_list:page_params.data_color,//颜色列表
        date_type_list:page_params.data_date_type,//有效期时间类型列表
        business_service_list:[//商家服务类型列表
            {'type':'BIZ_SERVICE_DELIVER','name':'外卖服务'},
            {'type':'BIZ_SERVICE_FREE_PARK','name':'停车位'},
            {'type':'BIZ_SERVICE_WITH_PET','name':'可带宠物'},
            {'type':'BIZ_SERVICE_FREE_WIFI','name':'免费wifi'},
        ],
        time_limit_weeks_list:null,
        member_custom_field_name_type_list:[
            {'type':'FIELD_NAME_TYPE_LEVEL','name':'等级',},
            {'type':'FIELD_NAME_TYPE_COUPON','name':'优惠券',},
            {'type':'FIELD_NAME_TYPE_STAMP','name':'印花',},
            {'type':'FIELD_NAME_TYPE_DISCOUNT','name':'折扣',},
            {'type':'FIELD_NAME_TYPE_ACHIEVEMEN','name':'成就',},
            {'type':'FIELD_NAME_TYPE_MILEAGE','name':'里程',},
            {'type':'FIELD_NAME_TYPE_SET_POINTS','name':'集点',},
            {'type':'FIELD_NAME_TYPE_TIMS','name':'次数',},
        ],
        member_activate_type:[
            {'type':'api','name':'接口激活',},
            {'type':'onekey','name':'一键激活',},
            {'type':'auto','name':'自动激活',},
        ],
        member_field_custom_type_list:[
            {'type':'FORM_FIELD_TEXT','name':'文本',},
            {'type':'FORM_FIELD_RADIO','name':'单选',},
            {'type':'FORM_FIELD_SELECT','name':'下拉选择',},
            {'type':'FORM_FIELD_CHECK_BOX','name':'多选',},
        ],
        member_field_init_data:{'type':'FORM_FIELD_TEXT','name':'','is_common':0,'is_open':1,'is_required':0,'values':[]},//会员卡字段初始数据
        member_field_cur_data:{'type':'FORM_FIELD_TEXT','name':'','values':[]},//当前会员卡字段编辑数据
        catNormalizer(node){
            return {
                id:node.wx_tag_id,
                label:node.tag_name,
                children: node.children,
            }
        },
    },
    watch : {
        cardData : {
            handler: function (val) {
                //卡券标识自动更新
                let cardType = this.cardData.base.card_type;
                if (cardType && page_params.data_card_key_group[cardType]){
                    this.card_key_list = page_params.data_card_key_group[cardType];
                }
                //激活方式
                if (this.cardData.member.activate.type == 'api'){
                    this.cardData.member.activate.activate_url = '';
                }else if (this.cardData.member.activate.type == 'onekey'){
                    this.cardData.member.activate.auto_activate = 0;
                    this.cardData.member.activate.wx_activate = 1;
                } else if (this.cardData.member.activate.type == 'auto'){
                    this.cardData.member.activate.auto_activate = 1;
                }
                //分享互斥
                if (this.cardData.advance.share_friends){
                    this.cardData.base.can_share = 0;
                    this.cardData.base.can_give_friend = 0;
                }
                if (this.cardData.base.can_share || this.cardData.base.can_give_friend){
                    this.cardData.advance.share_friends = 0;
                }
            },
            deep: true
        },
    },
    computed:{

    },
    mounted:function(){
        if (this.cardData.advance.time_limit.length > 0){
            this.time_limit_weeks_list = this.cardData.advance.time_limit;
        } else{
            this.cardData.advance.time_limit = this.time_limit_weeks_list;
        }
        //卡券标识更新
        let cardType = this.cardData.base.card_type;
        if (cardType && page_params.data_card_key_group[cardType]){
            this.card_key_list = page_params.data_card_key_group[cardType];
        }
    },
    methods:{
        changeTab:function(){
            //瀑布流重新渲染
            this.$nextTick(function(){
                this.$redrawVueMasonry();
            })
        },
        memberFieldAdd:function(){
            this.member_field_cur_data = _.cloneDeep(this.member_field_init_data);
            this.box = layer.open({
                type: 1,
                title: '添加开卡字段',
                anim: 2,
                shadeClose: true, //开启遮罩关闭
                skin: 'layui-layer-molv', //加上边框
                area: ['500px', '400px'], //宽高
                content: $('#box-member-field')
            });
        },
        memberFieldEdit:function(index){//编辑开卡字段
            this.member_field_cur_data = this.member_field_list[index];
            this.member_field_cur_data.index = index;
            this.box = layer.open({
                type: 1,
                title: '编辑开卡字段',
                anim: 2,
                shadeClose: true, //开启遮罩关闭
                skin: 'layui-layer-molv', //加上边框
                area: ['500px', '400px'], //宽高
                content: $('#box-member-field')
            });
        },
        memberFieldDel:function(index){//删除开卡字段
            layer.confirm('你确定删除该字段么？', {icon: 3, title: '删除提示',shadeClose: true,}, (boxIndex) => {
                this.member_field_list.splice(index, 1);
                layer.close(boxIndex);
            });
        },
        memberFieldSave:function(){
            try{
                if (this.member_field_cur_data.type == "") {
                    throw new Error('请选择富文本类型');
                }
                if (this.member_field_cur_data.name == "") {
                    throw new Error('请填写字段名');
                }
                if (this.member_field_cur_data.type != 'FORM_FIELD_TEXT' && _.isEmpty(this.member_field_cur_data.values)) {
                    throw new Error('字段选择项不能为空');
                }
            }catch (e) {
                utils.showToastr({
                    'status': 'error',
                    'title': '错误',
                    'message': e
                });
                return false;
            }
            if (this.member_field_cur_data.index){
                let index = this.member_field_cur_data.index;
                this.member_field_list[index] = _.cloneDeep(this.member_field_cur_data);
            } else{
                this.member_field_list.push(_.cloneDeep(this.member_field_cur_data))
            }
            layer.close(this.box);
        },
        save:function(){
            let loadBox = layer.load(1, {
                shade: [0.3,'#fff'] //0.1透明度的白色背景
            });
            let url_save = this.cardData.base.card_base_id ? page_params.url_edit : page_params.url_add;
            utils.ajaxPost(url_save, this.cardData, (response, data) => {
                layer.close(loadBox);
                utils.showToastr(response.data, 'go');
            });
        },
    },
    components: {
        InputTag,VueDatepickerLocal,
    },
});